package com.anysoft.adapter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.facebook.drawee.view.SimpleDraweeView;
import com.anysoft.R;
import com.anysoft.bean.QueryManagerMoneyViewListBean;
import com.anysoft.utils.StringUtils;

import java.util.List;

/**
 * 首页理财视点列表适配器
 * Created by yanbin on 2016-08-01.
 */
public class HomeFinancialNewAdapter extends BaseAdapter {

    private Context context;

    private List<QueryManagerMoneyViewListBean.DataBean.DataListBean> listFinancial;

    /**
     * 理财视点列表适配器
     *
     * @param context 上下问
     */
    public HomeFinancialNewAdapter(Activity context, List<QueryManagerMoneyViewListBean.DataBean.DataListBean> listFinancial) {
        this.context = context;
        this.listFinancial = listFinancial;
//        Log.e("adapter", listFinancial.size() + "");
    }


    @Override
    public int getCount() {
        if (listFinancial == null)
            return 0;
        return listFinancial.size();
    }

    @Override
    public Object getItem(int position) {
        return listFinancial.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.list_item_financial_new, null);
            holder = new ViewHolder();
            holder.tv_item_home_financial_label = (TextView) convertView.findViewById(R.id.tv_item_home_financial_label);
            holder.tv_item_home_financial_time = (TextView) convertView.findViewById(R.id.tv_item_home_financial_time);
            holder.tv_item_home_financial_title = (TextView) convertView.findViewById(R.id.tv_item_home_financial_title);
            holder.iv_item_home_financial = (ImageView) convertView.findViewById(R.id.iv_item_home_financial);
            holder.sdv_item_home_financial = (SimpleDraweeView) convertView.findViewById(R.id.sdv_item_home_financial);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_item_home_financial_title.setText(listFinancial.get(position).getHead() + "");
        String time = listFinancial.get(position).getCreateTime();
        if (!StringUtils.isNullOrEmpty(time)) {
            time = time.length() > 10 ? time.substring(5, 10) : time;
//            time = time.length() > 10 ? time.substring(0, 10) : time;
        }
        holder.tv_item_home_financial_time.setText(time + "");
        holder.tv_item_home_financial_label.setText("【" + listFinancial.get(position).getCategory() + "】");
        Glide.with(context).load(listFinancial.get(position).getCategoryImg()).placeholder(R.mipmap.banner_default).error(R.mipmap.banner_default).into
                (holder.iv_item_home_financial);
        Uri imageUri = Uri.parse(listFinancial.get(position).getCategoryImg());
        holder.sdv_item_home_financial.setImageURI(imageUri);
//        MentorLogger.Log("HomeFinancialNewAdapter", position + "-----" + listFinancial.get(position).getCategoryImg());
//        System.out.println("CategoryImg---" + listFinancial.get(position).getCategoryImg());

        return convertView;
    }


    public static class ViewHolder {
        /**
         * category
         */
        TextView tv_item_home_financial_label;
        /**
         * 日期
         */
        TextView tv_item_home_financial_time;
        /**
         * 标题
         */
        TextView tv_item_home_financial_title;
        /**
         * 图标
         */
        ImageView iv_item_home_financial;
        /**
         * 图标
         */
        SimpleDraweeView sdv_item_home_financial;
    }

}
